package cn.fbs.service;

import cn.fbs.dao.dm.DMAtlasInfoMapper;
import cn.fbs.model.TAtlasInfo;
import cn.fbs.model.TAtlasinfoTree;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yuyuesheng
 * @version 1.0
 * @date 2021/2/1 10:40
 */
@Service
@Slf4j
public class TAtlasTypeInfoService {


    @Autowired
    private DMAtlasInfoMapper mapper;

    public List<TAtlasinfoTree> findAll() {
        List<TAtlasinfoTree> list = mapper.selectAll();
        return list;
    }

    public List<TAtlasinfoTree> findAllByTree2() {
        List<TAtlasinfoTree> all = findAll();
        List<TAtlasinfoTree> list = new ArrayList<>();
        for (TAtlasinfoTree tAtlasinfoTree : all) {
            if (tAtlasinfoTree.getPid() == 0) {
                list.add(tAtlasinfoTree);
            }
            for (TAtlasinfoTree tree : all) {
                //用equals比较
                if (tree.getPid().equals(tAtlasinfoTree.getId()) && tAtlasinfoTree.getDataType() == 0) {
                    if (tAtlasinfoTree.getChildrens() == null) {
                        tAtlasinfoTree.setChildrens(new ArrayList<TAtlasinfoTree>());
                    }
                    tAtlasinfoTree.getChildrens().add(tree);
                }
            }
        }

        return list;
    }

    public List<TAtlasinfoTree> findAllByTree() {
        //1.查出所有分类
        List<TAtlasinfoTree> all = findAll();
        //2.组装成父子的树形结构
        List<TAtlasinfoTree> atlasTypeInfos = all.stream()
                .filter(tAtlasTypeInfo -> tAtlasTypeInfo.getPid() == 0)
                .map(menu -> {
                    menu.setChildrens(getChildrens(menu, all));
                    return menu;
                }).collect(Collectors.toList());
        return atlasTypeInfos;
    }

    //递归查找此类型下的子类型
    public List<TAtlasinfoTree> getChildrens(TAtlasinfoTree root, List<TAtlasinfoTree> all) {
        List<TAtlasinfoTree> list = null;
        //图册数据
        if (root.getDataType() == 0) {
            list = all.stream().filter((tAtlasTypeInfo) -> {
                return (root.getId().equals(tAtlasTypeInfo.getPid()) && tAtlasTypeInfo.getDataType() == 0);
            }).map((tAtlasTypeInfo) -> {
                tAtlasTypeInfo.setChildrens(getChildrens(tAtlasTypeInfo, all));
                return tAtlasTypeInfo;
            }).collect(Collectors.toList());
        }
        return list;
    }


    public String saveType(TAtlasInfo info) {
        String atlasName = info.getAtlasName();
        Integer pid = info.getPid();

        int count = mapper.selectCountByName(atlasName, pid);
        if (count > 0) {
            return "名称重复！";
        }
        mapper.saveType(info);
        return "新增成功!";
    }

    //更改类型
    public String updateType(TAtlasInfo info) {
        String atlasName = info.getAtlasName();
        Integer pid = info.getPid();
        int count = mapper.selectCountByName(atlasName, pid);
        if (count > 0) {
            return "名称重复！";
        }
        mapper.updateType(info);
        return "修改成功!";
    }

    public Map<String, Object> deleteType(String id) {
        //定义返回值
        Map<String, Object> resultMap = new HashMap<>();

        int i = mapper.deleteType(id);
        if (i > 0) {
            resultMap.put("status", "success");
            resultMap.put("code", 1);
            resultMap.put("message", "删除成功");
        } else {
            resultMap.put("status", "failed");
            resultMap.put("code", 0);
            resultMap.put("message", "删除失败");
        }
        return resultMap;
    }


    public List<TAtlasInfo> getCurrentPhotoAlbum(List<Integer> Ids,Integer dataType,String name) {

        String IDS = StringUtils.join(Ids, ",");
        List<TAtlasInfo> list = mapper.getCurrentPhotoAlbum(IDS,dataType,name);
        return list;
    }
}
